package com.mystyle.le.weekproject.sqlite;
/**
 * 增 删 查数据库
 */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbManager {
	private MySqlite dbUtils;

	public DbManager(Context context) {
		super();
		dbUtils = new MySqlite(context);
	}

	/**
	 *获取当前表中所有数据
	 * @return
	 */
	public Cursor getUsers(){
		SQLiteDatabase database = dbUtils.getReadableDatabase();

		return database.rawQuery("select * from data", null);

	}

	/**
	 * 插入数据
	 */
	public void insertData(String name,String ranking,String state,String id,String icon){
		SQLiteDatabase database = dbUtils.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("ranking", ranking);
		values.put("state", state);
		values.put("id", id);
		values.put("icon", icon);

		/**
		 * insert(String table, String nullColumnHack, ContentValues values)

		 */
		database.insert("data", null, values);
	}

	/**
	 * 修改数据到users表中
	 */
	public void updateData(int id,String name,String ranking,String state,String id1,String icon){
		//打开数据库链接
		SQLiteDatabase database = dbUtils.getReadableDatabase();
		//进行数据修改
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("ranking", ranking);
		values.put("state", state);
		values.put("id1", id1);
		values.put("icon", icon);
		/**
		 * (String table, ContentValues values, String whereClause, String[] whereArgs)
		 */
		database.update("data",values , "_id = ?", new String []{id+""});
	}

	/**
	 * 删除数据
	 */
	public void deleteData(int id){
		SQLiteDatabase database = dbUtils.getReadableDatabase();
		/**
		 * (String table, String whereClause, String[] whereArgs)
		 */
		database.delete("data", "_id = ?", new String[]{id+""});
	}
}
